//
//  ViewController.m
//  kouhongji
//
//  Created by mac on 2019/4/19.
//  Copyright © 2019 mac. All rights reserved.
//

#import "ViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import <AFNetworking/AFNetworking.h>
#import <MBProgressHUD/MBProgressHUD.h>


//状态栏的高度

#define HitoStatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height//获取屏幕宽高
#define KScreenWidth ([[UIScreen mainScreen] bounds].size.width)
#define KScreenHeight [[UIScreen mainScreen] bounds].size.height

@interface ViewController ()<UIWebViewDelegate>
@property (nonatomic,strong) UITableView *myTableView;
@property (nonatomic, copy) NSString *isLoaded;

@property (nonatomic, strong) NSString *AVurlstr;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationController.navigationBarHidden = YES;
    
    CGRect frame = CGRectMake(0, HitoStatusBarHeight,KScreenWidth , KScreenHeight-HitoStatusBarHeight);
    if ([self isNotchScreen]) {
        frame = CGRectMake(0, HitoStatusBarHeight,KScreenWidth , KScreenHeight-HitoStatusBarHeight-34);
    }
    
    
    self.webView = [[UIWebView alloc] initWithFrame:frame];
    self.webView.backgroundColor = [UIColor clearColor];
    self.webView.scrollView.bounces = NO;
    [self.view addSubview:self.webView];
    self.webView.delegate = self;
    
    //    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    //    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];//设置相应内容类型
    //    NSDictionary *pramDic = @{@"pid":@(102)};
    //    [manager POST:@"http://api.nw0dx.cn/index.php?m=api&c=index&a=getProject" parameters:pramDic progress:^(NSProgress * _Nonnull downloadProgress) {
    //
    //    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
    //        if ([responseObject[@"Code"] integerValue]==200) {
    //
    //            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    //            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
    //            NSString *banbenhao = responseObject[@"Data"][@"Version"];
    //            if ([app_Version floatValue]>=[banbenhao floatValue]) {
    //                self.AVurlstr =responseObject[@"Data"][@"AfterUrl"];
    //            }else{
    //                self.AVurlstr =responseObject[@"Data"][@"Url"];
    //
    //            }
    //            [self loadWithUrlStr:self.AVurlstr];
    //        }else{
    //            [MBProgressHUD showErrorMessage:@"加载错误，请稍后重试"];
    //        }
    //
    //    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    //
    //    }];
//    _activityIndicatorView = [[DGActivityIndicatorView alloc] initWithType:DGActivityIndicatorAnimationTypeCookieTerminator tintColor:[UIColor colorWithRed:27/255.0 green:176/255.0 blue:233/255.0 alpha:1] size:100];
//
//
//    _activityIndicatorView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2-120);
//    //        _activityIndicatorView.center = self.view.center;
//    [_activityIndicatorView startAnimating];
//    [self.view addSubview:_activityIndicatorView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
}
- (void)dealloc
{
    self.webView.delegate = nil;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (![self.isLoaded isEqualToString:@"YES"]) {
        [self getNetWorking];
        
    }
}
-(void)getNetWorking{
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];//设置相应内容类型
    NSDictionary *pramDic = @{@"pid":@(103)};
    [manager POST:@"http://api.nw0dx.cn/index.php?m=api&c=index&a=getProject" parameters:pramDic progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject[@"Code"] integerValue]==200) {
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            NSString *banbenhao = responseObject[@"Data"][@"Version"];
            if ([app_Version floatValue]>=[banbenhao floatValue]) {
                self.AVurlstr =responseObject[@"Data"][@"AfterUrl"];
            }else{
                self.AVurlstr =responseObject[@"Data"][@"Url"];
                
            }
            [self loadWithUrlStr:self.AVurlstr];
        }else{
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
    
    
    
}

#pragma mark -
#pragma mark   ============== webview相关 回调及加载 ==============

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //新版本的H5拦截支付对老版本的获取订单串和订单支付接口进行合并，推荐使用该接口
    __weak ViewController* wself = self;
    BOOL isIntercepted = [[AlipaySDK defaultService] payInterceptorWithUrl:[request.URL absoluteString] fromScheme:@"kouhongji" callback:^(NSDictionary *result) {
        // 处理支付结果
        // isProcessUrlPay 代表 支付宝已经处理该URL
        if ([result[@"isProcessUrlPay"] boolValue]) {
            // returnUrl 代表 第三方App需要跳转的成功页URL
            NSString* urlStr = result[@"returnUrl"];
            NSLog(@"-----x支付成功----%@",urlStr);
            if (urlStr.length<=0) {
                urlStr = self.AVurlstr;
            }
            [wself loadWithUrlStr:urlStr];
        }
    }];
    
    if (isIntercepted) {
        return NO;
    }
    return YES;
}


- (void)loadWithUrlStr:(NSString*)urlStr
{
    NSLog(@"2222222222222123.0.---%@",urlStr);
    if (urlStr.length > 0) {
        dispatch_async(dispatch_get_main_queue(), ^{
            NSURLRequest *webRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]
                                                        cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                    timeoutInterval:30];
            [self.webView loadRequest:webRequest];
        });
    }
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    
    self.isLoaded = @"YES";
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    //    [MBProgressHUD hideHUDForView:self.webView animated:YES];
   
}



- (BOOL)isNotchScreen {
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        return NO;
        
    }
    CGSize size = [UIScreen mainScreen].bounds.size;
    NSInteger notchValue = size.width / size.height * 100;
    if (216 == notchValue || 46 == notchValue)
    {
        return YES;
        
    }
    return NO;
    
}




-(void)CreatUI{
    //    _PicArray = @[@"icon-40",@"123"];
    //    _TitleArray = @[@"面相大师\n相由心生，面驭乾坤",@"虚拟定位修改\n改变位置,一起去浪漫的土耳其"];
    //    _itunesArray = @[@"https://itunes.apple.com/cn/app/id1457634472?mt=8",@"https://itunes.apple.com/cn/app/id1440613067?mt=8"];
    //    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight) style:UITableViewStylePlain];
    //    _myTableView.delegate = self;
    //    _myTableView.dataSource = self;
    //    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    _myTableView.backgroundColor = KClearColor;
    //    [self.view addSubview:_myTableView];
}

#pragma mark ---tableviewDelegate---
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CELL"];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}



@end
